package com.gy.training.ui;

import android.app.Activity;
import android.content.IntentFilter;
import android.os.Bundle;

import android.widget.EditText;

import com.gy.training.R;
import com.gy.training.p.SMSBroadcastReceiver;

public class SMSActivity extends Activity {
     private EditText verifyText;
     private SMSBroadcastReceiver smsBroadcastReceiver;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sms);
        verifyText = findViewById(R.id.verify_text);
        init();
    }

    @Override
    protected void onStop() {
        super.onStop();
        if(null!=smsBroadcastReceiver){
            unregisterReceiver(smsBroadcastReceiver);
        }
    }

    private void init(){
        smsBroadcastReceiver = new SMSBroadcastReceiver();
        IntentFilter filter = new IntentFilter("");
        filter.addAction("android.provider.Telephony.TextBasedSmsColumns");
        filter.addCategory("android.intent.category.DEFAULT");
        registerReceiver(smsBroadcastReceiver,filter);
        smsBroadcastReceiver.setOnReceivedMessageListener(new SMSBroadcastReceiver.MessageListener() {
            @Override
            public void OnReceived(String message) {
                verifyText.setText(message);
            }
        });
    }
}
